package com.gancp.service.impl;

import com.gancp.dao.IBaseMapper;
import com.gancp.dao.user.UserMapper;
import com.gancp.domain.User;
import com.gancp.service.UserBiz;
import com.gancp.service.base.BaseBizImpl;
import com.gancp.utills.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * Created by GCP on 2018/4/29.
 */
@Service(value = "UserBiz")
@Transactional
public class UserBizImpl extends BaseBizImpl<User, Long> implements UserBiz {

    @Autowired
    private UserMapper userMapper;

    @Override
    protected IBaseMapper getIBaseMapper() {
        return userMapper;
    }

    @Override
    public boolean checkUserByName(String username, String password) throws Exception {
        User user = userMapper.selectByUserName( username );
        if (user == null) {
            return false;
        }
        return Md5Utils.compare( password, user.getPassword() );
    }

    @Override
    public void insertUser(User user) throws Exception {
        String pwd = user.getPassword();
        String encryPwd = Md5Utils.enctyptCode( pwd.getBytes( "utf-8" ) );
        user.setPassword( encryPwd );
        userMapper.insertSelective( user );
    }

    @Override
    public User findUserByUserName(String username) {
        return userMapper.selectByUserName( username );
    }


}
